'use strict';

const Controller = require('egg').Controller;
const path = require('path');
const fs = require('fs');

class MasterController extends Controller {

  //添加
  async add() {
    try {
      const {ctx,service} = this;
      let {model,obj} = ctx.request.body;
      let result = await service.normal.addinfo(model,obj);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据id修改
  async reviseid() {
    try {
      const {ctx,service} = this;
      let {model,id,obj} = ctx.request.body;
      if(!id || !obj){
        ctx.body =  {data:0,content:error};
        return;
      }
      let result = await service.normal.statebyid(model,id,obj);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //多条件修改
  async revise() {
    try {
      const {ctx,service} = this;
      let {model,state,obj} = ctx.request.body;
      let result = await service.normal.statebyjson(model,state,obj);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //分页查询
  async getones() {
    try {
      const {ctx,service} = this;
      let {model,state,page,size,sorts=null} = ctx.request.body;
      let result = await service.normal.getinfos(model,state,page,size,sorts);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //条件查询多条
  async getlimits() {
    try {
      const {ctx,service} = this;
      let {model,state,page,size,sorts=null} = ctx.request.body;
      let result = await service.normal.getlimits(model,state,sorts);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据id查看唯一查询
  async getonebyid(){
    try {
      const {ctx,service} = this;
      let {model,id} = ctx.request.body;
      let result = await service.normal.getinfobyid(model,id);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据多条件唯一查询
  async getone(){
    try {
      const {ctx,service} = this;
      let {model,state} = ctx.request.body;
      let result = await service.normal.getinfo(model,state);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据添加查询数量
  async getsum(){
    try {
      const {ctx,service} = this;
      let {model,state} = ctx.request.body;
      let sum = await ctx.model[model].count(state); 
      ctx.body = {data: 1, sum};
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据id删除
  async deleteone(){
    try {
      const {ctx,service} = this;
      let {model,id} = ctx.request.body;
      let result = await service.normal.remTable(model,id);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }

  //根据多条件删除
  async deleteones(){
    try {
      const {ctx,service} = this;
      let {model,state} = ctx.request.body;
      let result = await service.normal.deleteMany(model,state);
      ctx.body = result;
    } catch (error) {
        console.log(error)
        ctx.body =  {data:0,content:error};
    }
  }
}

module.exports = MasterController;
